<template>
  <div>
    <el-card class="box-card">
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/allbgimg' }">轮播图列表</el-breadcrumb-item>
          <el-breadcrumb-item>添加轮播图</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <div class="rotationchart_box">
          <el-form v-model="rotationchartlist" label-position="top">
            <el-form-item label="轮播图标题:">
              <el-input v-model="rotationchartlist.title" placeholder="请输入轮播图的标题(2~10字内)"></el-input>
            </el-form-item>
            <el-form-item label="轮播图:">
              <div>
                <div class="cropper-content">
                  <div class="cropper">
                    <vueCropper ref="cropper" :img="option.img" :outputSize="option.size" :outputType="option.outputType" :info="true" :full="option.full" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original" :autoCrop="option.autoCrop" :autoCropWidth="option.autoCropWidth" :autoCropHeight="option.autoCropHeight" :fixedBox="option.fixedBox" @realTime="realTime">
                    </vueCropper>
                  </div>
                  <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden', 'margin': '5px'}">
                    <div :style="previews.div" class="preview">
                      <img :src="previews.url" :style="previews.img">
                    </div>
                  </div>
                </div>

                <div class="footer-btn">
                  <div class="scope-btn">
                    <label class="btn" for="uploads">更换图片</label>
                    <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/jpg" @change="uploadImg($event, 1)">

                    <button @click="changeScale(1)"><i class="el-icon-zoom-in"></i></button>
                    <button @click="changeScale(-1)"><i class="el-icon-zoom-out"></i></button>
                    <button @click="rotateLeft"><i class="el-icon-refresh-right"></i></button>
                    <button @click="rotateRight"><i class="el-icon-refresh-left"></i></button>
                  </div>
                  <div class="upload-btn">
                    <button type="primary" @click="uploadrc">上传<i class="el-icon-upload el-icon--right"></i></button>
                  </div>
                </div>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rotationchartlist: {
        title: '',
        image: ''
      },
      crap: false,
      previews: {},
      option: {
        img: '',
        size: 1,
        full: false, // 输出原图比例截图
        outputType: 'png',
        canMove: true,
        original: false,
        canMoveBox: true,
        autoCrop: true,
        autoCropWidth: 400,
        autoCropHeight: 200,
        fixedBox: true
      }
    }
  },
  methods: {
    // 修改图片大小
    changeScale(num) {
      num = num || 1
      this.$refs.cropper.changeScale(num)
    },
    // 向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft()
    },
    // 向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight()
    },
    // 实时预览函数
    realTime(data) {
      this.previews = data
    },
    // 保存资源封面
    uploadrc() {
      this.$refs.cropper.getCropData(async (data) => {
        // console.log(data)
        this.rotationchartlist.image = data
        const formdata = this.$qs.stringify(this.rotationchartlist)
        const res = await this.$http({
          method: 'post',
          url: '/slides',
          data: formdata
        })
        if (res.status !== 200) {
          return this.$message({
            showClose: true,
            message: '轮播图上传失败!',
            type: 'error'
          })
        }
        this.$message({
          showClose: true,
          message: '封面保存成功！',
          type: 'success'
        })
        this.$router.go(0)
      })
    },
    uploadImg(e, num) {
      // 上传图片
      // this.option.img
      const that = this
      var file = e.target.files[0]
      if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
        that.$message({
          showClose: true,
          message: '图片类型必须是.gif,jpeg,jpg,png,bmp中的一种!',
          type: 'error'
        })
        return false
      }
      var reader = new FileReader()
      reader.onload = (e) => {
        let data
        if (typeof e.target.result === 'object') {
          // 把Array Buffer转化为blob 如果是base64不需要
          data = window.URL.createObjectURL(new Blob([e.target.result]))
        } else {
          data = e.target.result
        }
        if (num === 1) {
          this.option.img = data
        } else if (num === 2) {
          this.example2.img = data
        }
      }
      // 转化为base64
      reader.readAsDataURL(file)
      // 转化为blob
      // reader.readAsArrayBuffer(file)
    }
  }
}
</script>

<style lang='less' scoped>
.rotationchart_box {
  margin: 10px;
}

.rotationchart_box .el-form-item__label {
  font-size: 16px;
}

.cropper-content{
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
  .cropper{
    width: 550px;
    height: 400px;
  }
  .show-preview{
    flex: 1;
    width: 400px;
    height: 200px;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    .preview{
      overflow: hidden;
      border:1px solid #cccccc;
      background: #cccccc;
      margin-left: 40px;
    }
  }
}

.footer-btn{
  margin-top: 30px;
  display: flex;
  display: -webkit-flex;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
  button {
    border: none;
    height: 40px;
    line-height: 40px;
  }
  .scope-btn{
    font-size: 14px;
    width: 350px;
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    -webkit-justify-content: space-between;
    button {
      width: 40px;
      font-size: 16px;
      border-radius: 3px;
    }
  }
  .upload-btn{
    flex: 1;
    -webkit-flex: 1;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    button {
      width: 100px;
      margin-left: 230px;
      padding: 0;
      border: 0;
      border-radius: 3px;
      background-color: #67c23a;
      color: #fff;
      font-size: 16px;
    }
  }
  .btn {
    outline: none;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    -webkit-appearance: none;
    text-align: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    outline: 0;
    margin: 0;
    -webkit-transition: .1s;
    transition: .1s;
    font-size: 16px;
    line-height: 40px;
    padding: 0 20px;
    border-radius: 3px;
    color: #fff;
    background-color: #409EFF;
    border-color: #409EFF;
  }
}
</style>
